package com.dameng.jianyouquan.mvp.model;

import android.content.Context;
import android.text.TextUtils;

import com.dameng.jianyouquan.api.Constant;
import com.dameng.jianyouquan.bean.LoginBean;
import com.dameng.jianyouquan.api.NetWorkManager;
import com.dameng.jianyouquan.utils.EdittextUtils;
import com.dameng.jianyouquan.utils.SpUtils;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class LoginModel {

    private String StrPhoneNum = "";
    private String StrPwd = "";


    public void login(Context context, String phoneNum, String pwd, OnLoginListener listener) {
        if (TextUtils.isEmpty(phoneNum) || TextUtils.isEmpty(pwd)) {
            listener.loginFail("手机号或者密码不能为空");
        } else if (!EdittextUtils.isMobileNO(phoneNum)) {
            listener.loginFail("请输入正确的手机号");
        } else if (!EdittextUtils.isPWD(pwd)) {
            listener.loginFail("密码格式不正确");
        } else {


            NetWorkManager.getService()
                    .getLogin(phoneNum, pwd, "")
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<LoginBean>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {
                            listener.loginFail(e.getMessage() + "");
                        }

                        @Override
                        public void onNext(LoginBean loginBean) {

                            int code = loginBean.getCode();
                            if (code == 200) {
                                listener.loginSuccess();
                                LoginBean.DataBean data = loginBean.getData();
                                SpUtils.setValue(context, Constant.SP_UID, data.getUserId());
//                                SpUtils.setValue(context, Constant.SP_TOKEN, data.getToken());
                                SpUtils.setValueInt(context, Constant.SP_ROLE_ID, data.getRoleId());
                                SpUtils.setValue(context, Constant.SP_USERNAME, phoneNum);
                                SpUtils.setValue(context, Constant.SP_USERPWD, pwd);
                            } else {
                                listener.loginFail(loginBean.getMsg() + "");
                            }
                        }
                    });
        }
    }





    public interface OnLoginListener {

        void loginSuccess();

        void loginFail(String msg);
    }


    public String getSessionCookie(String cookieString) {
        if (!TextUtils.isEmpty(cookieString)) {
            String[] splitCookie = cookieString.split(";");
            String[] splitSessionId = splitCookie[0].split("=");
            cookieString = splitSessionId[1];
            return cookieString;
        }
        return "";
    }
}
